home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: beginner question - constants
- Date: 04 Jan 1996 20:07:56 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Jan4210757@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <4ceht0$ruj@sun.cis.smu.edu>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: dbowman@post.smu.edu's message of 3 Jan 1996 12:28:48 -0600
-
- In article <4ceht0$ruj@sun.cis.smu.edu> dbowman@post.smu.edu (Damon Bowman) writes:
-
- Can anyone explain to me the functional difference between a
- macro-based constant and a formal constant? I understand that by
- using the #define directive, the preprocessor replaces all instances
- of the constant in the code with itÆs value. I also understand that
- formal constants (using the const keyword) do not undergo this
- replacement.
-
- What I donÆt understand is why one might be better than another and
- what conditions might influence me to choose one over the other. They
- appear to be two versions of the same thing.
-
- Is it a speed issue? Intuitively, the macro-based constant seems like
- it would process faster. WhatÆs the tradeoff?
-
- Macros are globaly substituted, they don't e.g. respect namespaces and
- therefore may result in unwanted substitutions. This makes it hard to
- use them in larger projects.
- Constants don't share these problems but can lead to minor speed-penalities
- if their values must be determined at run-time.
- If the values can be ascertained at compile-time the resulting code shouldn't
- be slower or larger code compared to the code using the appropriate macro
- definitions.
-
- Enno
-
-